// window.onload = function(){
// url网站域名
var url = "http://hengzhi.ayvvv.com";
// 分类三级至商品开始
var cate_id = getUrlParam('cate_id');

function product_ajax(cate_id, order = "") {
    $("#goods_s").empty();
    // 加载指定类型商品指定开始
    $.ajax({
        url: url + "/api/store/product/lst",
        dataType: "json",
        type: "get",
        data: {
            "cate_id": cate_id,
            "order": order
        },
        async: true,
        success: function (produt) {
            for (var i = 0; i < produt.data.list.length; i++) {
                $("#goods_s").append(`<li>
            <dl>
                <dt><a href="goods.html?product_id=` + produt.data.list[i].product_id + `"><img src="` + produt.data.list[i].image + `" alt="" /></a></dt>
                    <dd><a href="">` + produt.data.list[i].store_name + `</a></dt>
                    <dd><strong>￥` + produt.data.list[i].price + `</strong></dt>
                    
                </dl>
            </li>
        `);
            }
        },
        error: function () {}
    });
    // 加载指定类型商品指定结束                                   
};



function load_botton_class(bton_class_list) {
    $(".botton_class").empty();

    for (var k = 0; k < bton_class_list.length; k++) {
        if (k == 0) {
            $(".botton_class").append(`<dd flid="` + bton_class_list[k].store_category_id + `" class="product_s isselect"><a href="javasript:;">` + bton_class_list[k].cate_name + `</a></dd>`);
        } else {
            $(".botton_class").append(`<dd flid="` + bton_class_list[k].store_category_id + `" class="product_s"><a href="javasript:;">` + bton_class_list[k].cate_name + `</a></dd>`);
        }

    }

    $(".product_s").click(function () {
        var cate_id = $(this).attr('flid');
        $('.isselect').removeClass('isselect');
        $(this).addClass('isselect');
        product_ajax(cate_id);
    })
};

function list_ajax(argument) {
    $.ajax({
        url: url + "/api/store/product/category/lst",
        dataType: "json",
        type: "get",
        async: true,
        success: function (sort) {
            // console.log(sort.data);
            var topclass_list = sort.data;
            console.log(topclass_list)
            for (var i = 0; i < topclass_list.length; i++) {
                $('.catlist_wrap').append(`<div class="child">
                            <h3 class="hh3` + topclass_list[i].store_category_id + `"><b title="tpcid` + i + `"></b>` + topclass_list[i].cate_name + `</h3>
                            <ul class="none flid` + topclass_list[i].store_category_id + `"></ul>
                        </div>`);
                var class_list = topclass_list[i].children;

                for (var j = 0; j < ((class_list) ? (class_list.length) : (0)); j++) {
                    var flid = class_list[j].pid;
                    $(".flid" + flid).append(`<li flid="` + class_list[j].store_category_id + `" class="childes" title="tpcid` + i + `-btcid` + j + `"><a href="javasript:;">` + class_list[j].cate_name + `</a></li>`);
                }


            }

            $(".child h3").click(function () {
                $(this).toggleClass("on").parent().find("ul").toggle();

                var tpcidstr = $(this).children().attr('title');
                var tpcid = tpcidstr.substr(5, tpcidstr.length);

                load_botton_class(topclass_list[tpcid].children[0].children);

                product_ajax(topclass_list[tpcid].children[0].children[0].store_category_id);

            });
            $(".childes").click(function () {
                var allid = $(this).attr('title');
                var allid_list = allid.split('-');
                var tpcid = allid_list[0].substr(5, allid_list[0].length);
                var btcid = allid_list[1].substr(5, allid_list[1].length);
                // console.log(tpcid);
                // console.log(btcid);
                var bton_class_list = topclass_list[tpcid].children[btcid].children;

                load_botton_class(bton_class_list)

                product_ajax(bton_class_list[0].store_category_id);

            });

            if (argument === "select") {
                for (var s = 0; s < sort.data.length; s++) {
                    for (var d = 0; d < sort.data[s].children.length; d++) {
                        for (var f = 0; f < sort.data[s].children[d].children.length; f++) {
                            if (sort.data[s].children[d].children[f].store_category_id == cate_id) {
                                $(".hh3" + sort.data[s].store_category_id).attr("class", "on")
                                $(".flid" + sort.data[s].store_category_id).css('display', 'block');
                                load_botton_class(sort.data[s].children[d].children)
                            }

                        }
                    }

                }
            }

            if (argument === "first") {
                $(".child h3").first().trigger("click");

            } else {

            }


        },
        error: function () {}
    })

};


// 分类三级至商品结束

if (cate_id) {
    list_ajax("select");
    product_ajax(cate_id);



} else {
    list_ajax("first");

}

















// }